package com.netstar.salesman_90dsp.adapter;

import java.util.List;

import android.content.Context;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.netstar.salesman_90dsp.R;
import com.netstar.salesman_90dsp.model.TodayWorkModel;

/**
 * 今日工作列表适配器
 * 
 * @author zhangshuai
 * 
 */
public class ToadayWorkXLVAdapter extends BaseAdapter {
	/**
	 * 上下文
	 * 
	 * @param toadayWorkModelGrouplist
	 */
	private Context context;
	/**
	 * 工作数据， model集合
	 */
	private List<TodayWorkModel> todayWorkModelList;
	/**
	 * 临时对象
	 */
	private TodayWorkModel todayWorkModelTmp;

	public ToadayWorkXLVAdapter(Context context,
			List<TodayWorkModel> todayWorkModelList) {
		super();
		this.context = context;
		this.todayWorkModelList = todayWorkModelList;
	}

	@Override
	public int getCount() {
		return todayWorkModelList.size();
	}

	@Override
	public Object getItem(int arg0) {
		return todayWorkModelList.get(arg0);
	}

	@Override
	public long getItemId(int arg0) {
		return arg0;
	}

	@Override
	public View getView(int arg0, View arg1, ViewGroup arg2) {
		Holder holder = null;
		if (arg1 == null || arg1.getTag() == null) {
			arg1 = View.inflate(context,
					R.layout.activity_today_work_list_item, null);
			holder = new Holder();
			arg1.setTag(holder);
			holder.activity_today_work_list_item_ll_0 = arg1
					.findViewById(R.id.activity_today_work_list_item_ll_0);
			holder.activity_today_work_list_item_ll_1 = arg1
					.findViewById(R.id.activity_today_work_list_item_ll_1);
			holder.activity_today_work_list_item_tv_content_0 = (TextView) arg1
					.findViewById(R.id.activity_today_work_list_item_tv_content_0);
			holder.activity_today_work_list_item_tv_content_1 = (TextView) arg1
					.findViewById(R.id.activity_today_work_list_item_tv_content_1);
		} else {
			holder = (Holder) arg1.getTag();
		}
		todayWorkModelTmp = (TodayWorkModel) getItem(arg0);
		/*
		 * 判断是假的工作，是日期，还是真的工作， 日期的id是null 通过隐藏不同的view来切换布局
		 */
		if (TextUtils.isEmpty(todayWorkModelTmp.getId())) {
			// 是日期
			holder.activity_today_work_list_item_ll_0
					.setVisibility(View.VISIBLE);
			holder.activity_today_work_list_item_ll_1.setVisibility(View.GONE);
			holder.activity_today_work_list_item_tv_content_0
					.setText(todayWorkModelTmp.getPlanDateOnlyDate());
			arg1.setTag(R.integer.activity_today_work_list_apater_tag_key, null);
		} else {
			// 是工作
			holder.activity_today_work_list_item_ll_0.setVisibility(View.GONE);
			holder.activity_today_work_list_item_ll_1
					.setVisibility(View.VISIBLE);
			holder.activity_today_work_list_item_tv_content_1
					.setText(todayWorkModelTmp.getPlans());
			arg1.setTag(R.integer.activity_today_work_list_apater_tag_key,
					todayWorkModelTmp);
			// 判断任务是否完成 工作完成状态，：state(0:未完成；1:完成)，默认为0
			if ("1".equals(todayWorkModelTmp.getState())) {
				holder.activity_today_work_list_item_tv_content_1
						.setBackgroundColor(context
								.getResources()
								.getColor(
										R.color.activity_today_work_list_item_state_complete));
			} else {
				holder.activity_today_work_list_item_tv_content_1
						.setBackgroundColor(context
								.getResources()
								.getColor(
										R.color.activity_today_work_list_item_state_not_complete));
			}
		}
		return arg1;
	}

	public class Holder {
		public View activity_today_work_list_item_ll_0;
		public TextView activity_today_work_list_item_tv_content_0;
		public View activity_today_work_list_item_ll_1;
		public TextView activity_today_work_list_item_tv_content_1;
	}
}
